package com.spstudio.iforward.front;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.spstudio.iforward.core.qualifier.SPCoreLogged;

/**
 * Session Bean implementation class SPCoreDaemon
 */
@Singleton
@LocalBean
@Startup
public class SPCoreDaemon {

    /**
     * Default constructor. 
     */
    public SPCoreDaemon() {
    }

	@Inject @SPCoreLogged Logger log;
	private static InitialContext initialContext;
		
	@PostConstruct
	public void postConstruct(){
		log.info("### SPCoreDaemon start. ###");
	}
	
	@PreDestroy
	public void preDestory(){
		log.info("### SPCoreDaemon end. ###");
	}
	
	public Object lookup(String jndi) throws NamingException{
		return getInitialContext().lookup(jndi);
	}
	
	private InitialContext getInitialContext(){
		if (initialContext == null){
			try{
				initialContext = new InitialContext();
			}catch(Exception e){
				log.log(Level.SEVERE, e.getMessage());
			}
		}
		return initialContext;
	}
}
